<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Http\Controllers\BaseController;
use App\Http\Validate\OpenShop\StartBusiness\StartBusinessValidate;
use App\Service\OpenShop\Feishu\StartBusinessService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class StartBusinessController extends BaseController
{
    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    public function submit(Request $request, string $orderNo): JsonResponse
    {
        $requestParams = $request->merge(["orderNo" => $orderNo])->all();
        $params = StartBusinessValidate::submitValidate($requestParams);
        $attach = StartBusinessValidate::attachValidate($requestParams);
        StartBusinessService::submit(getRmsUserInfoByAuth(), $params, $attach);
        return $this->success();
    }

    /**
     * 分页提交
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function submitPart(Request $request, string $orderNo): JsonResponse
    {
        $finish        = (int)(bool)$request->get('finish');
        $requestParams = $request->merge(["orderNo" => $orderNo])->all();

        $params = StartBusinessValidate::submitValidate($requestParams, true);
        $attach = StartBusinessValidate::attachValidate($requestParams);

        if ($finish) {
            StartBusinessService::submit(getRmsUserInfoByAuth(), $params, $attach);
        } else {
            StartBusinessService::submitPart(getRmsUserInfoByAuth(), $params, $attach);
        }

        return $this->success();
    }

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    public function update(Request $request, string $orderNo): JsonResponse
    {
        $requestParams = $request->merge(["orderNo" => $orderNo])->all();
        $params = StartBusinessValidate::updateValidate($requestParams);
        $attach = StartBusinessValidate::attachValidate($requestParams);
        StartBusinessService::update(getRmsUserInfoByAuth(), $params, $attach);
        return $this->success();
    }

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    public function detail(Request $request, string $orderNo): JsonResponse
    {
        StartBusinessValidate::detailValidate($request->merge(["orderNo" => $orderNo])->all());
        return $this->success(StartBusinessService::detail($orderNo));
    }

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function detailInput(Request $request, string $orderNo): JsonResponse
    {
        StartBusinessValidate::detailValidate($request->merge(["orderNo" => $orderNo])->all());

        $lists = StartBusinessService::detailInput($orderNo);
        foreach ($lists as &$v) {
            $v = ['input' => $v];
        }

        return $this->success($lists);
    }
}
